package edu.fit5046.mymonashmate.utils;

import android.annotation.SuppressLint;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

public class DetailInfo {

	public static final int INFO_NAME = 0;
	public static final int INFO_SURNAME = INFO_NAME + 1;
	public static final int INFO_COURSE = INFO_SURNAME + 1;
	public static final int INFO_UNIT = INFO_COURSE + 1;
	public static final int INFO_NATIONNANITY = INFO_UNIT + 1;
	public static final int INFO_FIRSTLANGUAGE = INFO_NATIONNANITY + 1;
	public static final int INFO_SECONDLANGUAGE = INFO_FIRSTLANGUAGE + 1;
	public static final int INFO_ADDRESS = INFO_SECONDLANGUAGE + 1;
	public static final int INFO_FAVOURITEFOOD = INFO_ADDRESS + 1;
	public static final int INFO_FAVOURITEMOIVE = INFO_FAVOURITEFOOD + 1;
	public static final int INFO_FAVOURITEUNIT = INFO_FAVOURITEMOIVE + 1;
	public static final int INFO_FAVOURITEPROLAN = INFO_FAVOURITEUNIT + 1;
	public static final int INFO_CURRENTJOB = INFO_FAVOURITEPROLAN + 1;
	public static final int INFO_PREVIOUSJOB = INFO_CURRENTJOB + 1;

	public static final int INFO_STUID = INFO_PREVIOUSJOB + 1;
	public static final int INFO_PASSWORD = INFO_STUID + 1;
	public static final int INFO_LATITUDE = INFO_PASSWORD + 1;
	public static final int INFO_LONGITUDE = INFO_LATITUDE + 1;

	// public static final int INFO_COUNT = INFO_PREVIOUSJOB + 1;

	public int ID;
	public String password;
	public double latitude;
	public double longitude;
	public String name;
	public String surName;
	public String course;
	public String unit;
	public String nationNanity;
	public String firstLanguage;
	public String secondLanguage;
	public String address;
	public String favouriteFood;
	public String favouriteMovie;
	public String favouriteUnit;
	public String favouriteProgremLanguage;
	public String currentJob;
	public String previousJob;
	public int matching_score;

	public DetailInfo(SimpleInfo info) {
		this.ID = info.ID;
		this.latitude = info.latitude;
		this.longitude = info.longitude;
		this.name = info.Name;
		this.course = info.course;
	}

	public DetailInfo(String json) throws JSONException {
		JSONObject js = new JSONObject(json);
		this.ID = js.getInt("stuId");
		// cannot be null
		this.name = js.getString("givenname");
		this.surName = js.getString("surname");
		this.course = js.getString("course");
		this.unit = js.getString("unit");
		// location
		this.latitude = js.getDouble("latitude");
		this.longitude = js.getDouble("longitude");
		// hobbies & interests

		this.favouriteFood = js.getString("foodlike");
		this.favouriteMovie = js.getString("movielike");
		this.favouriteProgremLanguage = js.getString("pglanglike");
		this.favouriteUnit = js.getString("unitlike");
		this.nationNanity = js.getString("nationality");
		this.firstLanguage = js.getString("fstlang");
		this.secondLanguage = js.getString("sndlang");
		this.address = js.getString("suburb");
		this.currentJob = js.getString("job");
		this.previousJob = js.getString("prejob");
	}

	public DetailInfo() {
	}

	public void SetInfo(int index, String value) {
		switch (index) {
		case INFO_STUID:
			this.ID = Integer.valueOf(value);
			break;
		case INFO_PASSWORD:
			this.password = value;
			break;
		case INFO_NAME:
			this.name = value;
			break;
		case INFO_SURNAME:
			this.surName = value;
			break;
		case INFO_COURSE:
			this.course = value;
			break;
		case INFO_UNIT:
			this.unit = value;
			break;
		case INFO_NATIONNANITY:
			this.nationNanity = value;
			break;
		case INFO_FIRSTLANGUAGE:
			this.firstLanguage = value;
			break;
		case INFO_SECONDLANGUAGE:
			this.secondLanguage = value;
			break;
		case INFO_ADDRESS:
			this.address = value;
			break;
		case INFO_FAVOURITEFOOD:
			this.favouriteFood = value;
			break;
		case INFO_FAVOURITEMOIVE:
			this.favouriteMovie = value;
			break;
		case INFO_FAVOURITEUNIT:
			this.favouriteUnit = value;
			break;
		case INFO_FAVOURITEPROLAN:
			this.favouriteProgremLanguage = value;
			break;
		case INFO_CURRENTJOB:
			this.currentJob = value;
			break;
		case INFO_PREVIOUSJOB:
			this.previousJob = value;
			break;
		default:
			break;
		}
	}

	public String getValue(int index) {
		switch (index) {
		case INFO_STUID:
			return String.valueOf(this.ID);
		case INFO_PASSWORD:
			return this.password;
		case INFO_NAME:
			return this.name;
		case INFO_SURNAME:
			return this.surName;
		case INFO_COURSE:
			return this.course;
		case INFO_UNIT:
			return this.unit;
		case INFO_NATIONNANITY:
			return this.nationNanity;
		case INFO_FIRSTLANGUAGE:
			return this.firstLanguage;
		case INFO_SECONDLANGUAGE:
			return this.secondLanguage;
		case INFO_ADDRESS:
			return this.address;
		case INFO_FAVOURITEFOOD:
			return this.favouriteFood;
		case INFO_FAVOURITEMOIVE:
			return this.favouriteMovie;
		case INFO_FAVOURITEUNIT:
			return this.favouriteUnit;
		case INFO_FAVOURITEPROLAN:
			return this.favouriteProgremLanguage;
		case INFO_CURRENTJOB:
			return this.currentJob;
		case INFO_PREVIOUSJOB:
			return this.previousJob;
		default:
			return null;
		}

	}

	public Map<String, String> toMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("ID", String.valueOf(this.ID));
		map.put("password", this.password);
		map.put("latitude", String.valueOf(this.latitude));
		map.put("longitude", String.valueOf(this.longitude));
		map.put("name", this.name);
		map.put("surName", this.surName);
		map.put("course", this.course);
		map.put("unit", this.unit);
		map.put("nationNanity", this.nationNanity);
		map.put("firstLanguage", this.firstLanguage);
		map.put("secondLanguage", this.secondLanguage);
		map.put("address", this.address);
		map.put("favouriteFood", this.favouriteFood);
		map.put("favouriteMovie", this.favouriteMovie);
		map.put("favouriteUnit", this.favouriteUnit);
		map.put("favouriteProgremLanguage", this.favouriteProgremLanguage);
		map.put("currentJob", this.currentJob);
		map.put("previousJob", this.previousJob);

		return map;
	}

	public JSONObject toJson() throws JSONException {
		JSONObject json = new JSONObject();
		json.put("stuId", this.ID);
		json.put("latitude", this.latitude);
		json.put("longitude", this.longitude);
		json.put("givenname", this.name);
		json.put("surname", this.surName);
		json.put("course", this.course);
		json.put("unit", this.unit);
		

		json.put("fstlang", this.firstLanguage);
		json.put("nationality", this.nationNanity);
		json.put("sndlang", this.secondLanguage);
		json.put("suburb", this.address);
		json.put("foodlike", this.favouriteFood);
		json.put("movielike", this.favouriteMovie);
		json.put("unitlike", this.favouriteUnit);
		json.put("pglanglike", this.favouriteProgremLanguage);
		json.put("job", this.currentJob);
		json.put("prejob", this.previousJob);
		return json;
	}

	@SuppressLint("NewApi")
	public static DetailInfo toDetailInfo(Map<String, String> map) {
		DetailInfo info = new DetailInfo();
		String tmpString;
		tmpString = map.get("ID");
		if (tmpString == null) {
			info.ID = 0;
		} else {
			info.ID = Integer.valueOf(tmpString);
		}

		info.password = map.get("password");

		tmpString = map.get("latitude");
		if (tmpString == null) {
			info.latitude = 0;
		} else {
			info.latitude = Double.valueOf(tmpString);
		}

		tmpString = map.get("longitude");
		if (tmpString == null) {
			info.longitude = 0;
		} else {
			info.longitude = Double.valueOf(tmpString);
		}

		info.name = map.get("name");
		info.surName = map.get("surName");
		info.course = map.get("course");
		info.unit = map.get("unit");
		info.nationNanity = map.get("nationNanity");
		info.firstLanguage = map.get("firstLanguage");
		info.secondLanguage = map.get("secondLanguage");
		info.address = map.get("address");
		info.favouriteFood = map.get("favouriteFood");
		info.favouriteMovie = map.get("favouriteMovie");
		info.favouriteUnit = map.get("favouriteUnit");
		info.favouriteProgremLanguage = map.get("favouriteProgremLanguage");
		info.currentJob = map.get("currentJob");
		info.previousJob = map.get("previousJob");

		tmpString = map.get("matching_score");
		if (tmpString == null) {
			info.matching_score = 0;
		} else {
			info.matching_score = Integer.valueOf(tmpString);
		}

		return info;
	}
}
